JSON in Javascript
    • JSON stands for JavaScript Object Notation.
    • JSON is a text format for storing and transporting data
    • JSON is "self-describing" and easy to understand.
    • JSON is used to send data between computers.
    • JSON is language independent.

JSON Example

Valid Data Types
In JSON, values must be one of the following data types:

Examples

Object

const students = {
    name: 'Mohamed Dalmar',
    Grade: 'Graduated',
    Location: 'Mogadisho',
    Age: 45
}

JSON 


{
 "Student" :  {
   "name" : "Mohamed Dek",
   "lastname" : null,
   "Grade" : "Graduated" ,
   "Location" : "Mogadisho" ,
   "Age" : 25,
   "isHeStudent" : false
}
}


JSON Strings
Strings in JSON must be written in double quotes.
{"name" : "John"}

JSON Numbers
Numbers in JSON must be an integer or a floating point.
{"age" : 30}

JSON Objects
Values in JSON can be objects.
{
"employee": {"name":"John", "age":30, "city":"New York"}
}

Objects as values in JSON must follow the JSON syntax.

JSON Arrays
Values in JSON can be arrays.
{
"employees": ["John", "Anna", "Peter"]
}

JSON Booleans
Values in JSON can be true/false.
{"sale": true}

JSON null
Values in JSON can be null.
{"middlename": null}

Why Use JSON?
The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into JavaScript objects.

Since the format is text only, JSON data can easily be sent between computers, and used by any programming language.

JavaScript has a built in function for converting JSON strings into JavaScript objects:
JSON.parse()

JavaScript also has a built in function for converting an object into a JSON string:
JSON.stringify()

Storing Data
When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.

JSON makes it possible to store JavaScript objects as text.